package com.example.javafx.controller

import com.example.javafx.bilibilidownload.contextmenu.ContextMenuUtil
import com.example.javafx.bilibilidownload.pojo.BiliFavoriteSimplePojo
import com.example.javafx.bilibilidownload.pojo.BiliFocusPojo
import com.example.javafx.bilibilidownload.pojo.BiliUpPojo
import com.example.javafx.bilibilidownload.pojo.BiliVideoPojo
import com.example.javafx.bilibilidownload.util.BiliFileUtil.*
import com.example.javafx.bilibilidownload.util.BiliUtil.makeVideoNameByCreateTime
import com.example.javafx.bilibilidownload.util.CacheUtil
import com.example.javafx.bilibilidownload.util.DateUtil
import javafx.application.Platform
import javafx.fxml.FXML
import javafx.scene.control.ScrollPane
import javafx.scene.control.TreeView
import javafx.scene.control.cell.TextFieldTreeCell
import javafx.scene.input.MouseButton
import javafx.scene.layout.BorderPane
import javafx.util.StringConverter
import javafx.util.Callback

class HomeController {

    @FXML
    lateinit var tree: TreeView<Any>

    @FXML
    lateinit var treePane: ScrollPane

    @FXML
    lateinit var rootPane: BorderPane

    @FXML
    fun initialize() {

        var convert = object : StringConverter<Any>() {
            override fun toString(item: Any?): String {
                return when (item) {
                    is BiliFocusPojo -> {
                        item.focusName
                    }

                    is BiliUpPojo -> {
                        val upDownloadVideoSize = getUpDownloadVideoSize(item)
                        "${item.upName} (${upDownloadVideoSize})"
                    }

                    is BiliVideoPojo -> {
                        makeVideoNameByCreateTime(item)
                        if ((item.favoriteName.isNullOrEmpty() || item.favoriteTime.isNullOrEmpty()) && !item.created.isNullOrEmpty()) {
                            "${DateUtil.getTimesString(item.created.toLong() * 1000L)}(${getVideoDownloadFileSize(item)}) ${item.title}"
                        } else if (!item.favoriteTime.isNullOrEmpty()) {
                            "${DateUtil.getTimesString(item.favoriteTime.toLong() * 1000L)}(${getVideoDownloadFileSize(item)}) ${item.title}"
                        } else {
                            "${item.title}"
                        }

                    }

                    is BiliFavoriteSimplePojo -> {
                        val favoriteDownloadVideoSize = getFavoriteDownloadVideoSize(item)
                        "${item.title} (${favoriteDownloadVideoSize})"
                    }

                    is String -> {
                        item
                    }

                    else -> {
                        item.toString()
                    }
                }
            }

            override fun fromString(string: String?): Any {
                TODO("Not yet implemented")
            }
        }

        tree.cellFactory = Callback {
            object : TextFieldTreeCell<Any>(convert) {
                init {
                    setOnMouseClicked { event ->
                        if (event.button == MouseButton.SECONDARY && !isEmpty) {
                            when (item) {

                                is String -> {
                                    item == "我的关注"
                                    contextMenu = ContextMenuUtil.getFocusMenu()
                                }

                                is BiliFocusPojo -> {
                                    contextMenu = ContextMenuUtil.getFocusGroupMenu()

                                }

                                is BiliFavoriteSimplePojo -> {
                                    contextMenu = ContextMenuUtil.getFavoriteGroupMenu()
                                }

                                is BiliUpPojo -> {
                                    contextMenu = ContextMenuUtil.getUpMenu()
                                }

                                is BiliVideoPojo -> {
                                    contextMenu = ContextMenuUtil.getVideoMenu()
                                }

                            }
                            Platform.runLater {
                                contextMenu?.show(this, event.screenX, event.screenY)
                            }
                        }
                    }
                }

//                override fun updateItem(item: Any?, empty: Boolean) {
//                    super.updateItem(item, empty)
//
//                    if (empty || item == null) {
//                        setText(null);
//                        setGraphic(null);
//                    } else {
                /*                        when (item) {
                                            is String -> {
                                                if (item == "我的关注") {
                                                    style = "-fx-background-color: lightblue;" // 设置背景颜色
                                                } else {
                                                    style = "-fx-background-color: lightgray;"
                                                }
                                            }

                                            is BiliFocusPojo -> {
                                                style = "-fx-background-color: lightgreen;"
                                            }

                                            is BiliUpPojo -> {
                                                style = "-fx-background-color: lightyellow;"
                                            }

                                            is BiliVideoPojo -> {
                                                style = "-fx-background-color: lightpink;"
                                            }

                                            else -> {
                                                style = "" // 恢复默认样式
                                            }
                                        }*/

//                    }
//                }
            }
        }

    }
}